//
//  ChartTableViewController.swift
//  beegin2
//
//  Created by zsy on 16/4/4.
//  Copyright © 2016年 kszz. All rights reserved.
//

import UIKit

class ChartTableViewController: UITableViewController,UICollectionViewDataSource,UICollectionViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
        Common.rootViewController.mainTabBarController.tabBar.hidden = true
        
        tableView.registerNib(UINib(nibName: "TipViewCell", bundle: nil), forCellReuseIdentifier: "tipViewCell")
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func leftNavClick(sender: AnyObject) {
        
        self.navigationController?.popViewControllerAnimated(true)
        Common.rootViewController.mainTabBarController.tabBar.hidden = false
    }

    // MARK: - Table view data source
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
        
        if indexPath.section == 0 && indexPath.row == 0{
            
            //self.performSegueWithIdentifier("showMilkBrandSegue", sender: self)
        }
        
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 4
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return section != 3 ? 2 : 1
    }
    
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0{
        
            return 44
        }
        return 1
    }
    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        switch indexPath.section{
            
        case 0,1,2:
            return indexPath.row == 0 ? 262 : 60
        case 3:
            return 395
        default:
            return 0
        }
    }
    
    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if(section == 0){
            let headerCell = tableView.dequeueReusableCellWithIdentifier("remaindheaderCell")
            return headerCell
        }

        return nil
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let idArry = [["chartCell","sumCell"],["chartCell","chartTipCell"],["chartCell","chartTipCell"],["chartCollectionCell"]]
        let cell = tableView.dequeueReusableCellWithIdentifier(idArry[indexPath.section][indexPath.row], forIndexPath: indexPath)

        // Configure the cell...
        switch indexPath.section {
        case 0:
            if indexPath.row == 0{
               
                (cell as? ChartTableViewCell)?.showType = .sumLine
                (cell as? ChartTableViewCell)?.titleLable.text = "奶量摄入"
                (cell as? ChartTableViewCell)?.layoutIfNeeded()
                (cell as? ChartTableViewCell)?.chartView.showInView(cell)
                
            }
        case 1:
            if indexPath.row == 0{
                
                (cell as? ChartTableViewCell)?.titleLable.text = "维生素群摄入(%)"
                (cell as? ChartTableViewCell)?.showType = .vitaminBar
                (cell as? ChartTableViewCell)?.layoutIfNeeded()
                (cell as? ChartTableViewCell)?.chartView.showInView(cell)
                
            }else{
            
                (cell as? TipTableViewCell)?.tipbackView.layer.cornerRadius = 7
                (cell as? TipTableViewCell)?.tipbackView.backgroundColor = UIColor(red: 252 / 255, green: 110 / 255, blue: 80 / 255, alpha: 250 / 255)
            
            }
        case 2:
            if indexPath.row == 0{
                (cell as? ChartTableViewCell)?.titleLable.text = "微量元素群摄入(%)"
                (cell as? ChartTableViewCell)?.showType = .elementsBar
                (cell as? ChartTableViewCell)?.layoutIfNeeded()
                (cell as? ChartTableViewCell)?.chartView.showInView(cell)
                
            }else{
                
                (cell as? TipTableViewCell)?.tipbackView.layer.cornerRadius = 7
                (cell as? TipTableViewCell)?.tipbackView.backgroundColor = UIColor(red: 78 / 255, green: 193 / 255, blue: 233 / 255, alpha: 250 / 255)
                
            }
            
        case 3:
            (cell as? ElementCountTableViewCell)?.normalTipView.layer.cornerRadius = 5
            (cell as? ElementCountTableViewCell)?.highTipView.layer.cornerRadius = 5
            (cell as? ElementCountTableViewCell)?.lowTipView.layer.cornerRadius = 5

        default:
            break
        }
        
        return cell
    }
    

    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */
    
    
     // MARK: - collection view data source
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return 12
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        
        let data:[(count:String,name:String,level:elementLevel)] = [("65μg","叶酸",.normal),
                                                                    ("2mg","2mg",.normal),
                                                                    ("0.5g","DHA",.low),
                                                                    ("4.4g","ARA",.low),
                                                                    ("9.1g","蛋白质",.normal),
                                                                    ("60g","碳水化合物",.normal),
                                                                    ("31g","脂肪",.high),
                                                                    ("5μg","生物素",.normal),
                                                                    ("125mg","胆碱",.normal),
                                                                    ("0.3mg","核黄素",.normal),
                                                                    ("4.5g","亚麻酸",.normal),
                                                                    ("5.5g","亚油酸",.normal)]
                
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("elementCountCell", forIndexPath: indexPath) as! ElementCollectionViewCell
        
        cell.countLabel.text = data[indexPath.row].count
        cell.nameLabel.text = data[indexPath.row].name
        cell.level = data[indexPath.row].level
        
        cell.layoutIfNeeded()
        cell.backView.layer.cornerRadius = 7
        
        return cell
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
